﻿using System;
using System.Collections.Generic;
using System.IO;
using GeoAPI.Geometries;
using NetTopologySuite.Algorithm;
using NetTopologySuite.GeometriesGraph;

namespace NetTopologySuite.Planargraph
{
	public class DirectedEdge : GraphComponent, IComparable
	{
		public static IList<Edge> ToEdges(IList<DirectedEdge> dirEdges)
		{
			IList<Edge> list = new List<Edge>();
			foreach (DirectedEdge directedEdge in dirEdges)
			{
				list.Add(directedEdge.parentEdge);
			}
			return list;
		}

		public DirectedEdge(Node from, Node to, Coordinate directionPt, bool edgeDirection)
		{
			this.from = from;
			this.to = to;
			this.EdgeDirection = edgeDirection;
			this.p0 = from.Coordinate;
			this.p1 = directionPt;
			double num = this.p1.X - this.p0.X;
			double num2 = this.p1.Y - this.p0.Y;
			this._quadrant = QuadrantOp.Quadrant(num, num2);
			this._angle = Math.Atan2(num2, num);
		}

		public Edge Edge
		{
			get
			{
				return this.parentEdge;
			}
			set
			{
				this.parentEdge = value;
			}
		}

		public int Quadrant
		{
			get
			{
				return this._quadrant;
			}
		}

		public Coordinate DirectionPt
		{
			get
			{
				return this.p1;
			}
		}

		public bool EdgeDirection { get; protected set; }

		public Node FromNode
		{
			get
			{
				return this.from;
			}
		}

		public Node ToNode
		{
			get
			{
				return this.to;
			}
		}

		public Coordinate Coordinate
		{
			get
			{
				return this.from.Coordinate;
			}
		}

		public double Angle
		{
			get
			{
				return this._angle;
			}
		}

		public DirectedEdge Sym
		{
			get
			{
				return this._sym;
			}
			set
			{
				this._sym = value;
			}
		}

		public int CompareTo(object obj)
		{
			DirectedEdge e = (DirectedEdge)obj;
			return this.CompareDirection(e);
		}

		public int CompareDirection(DirectedEdge e)
		{
			if (this._quadrant > e.Quadrant)
			{
				return 1;
			}
			if (this._quadrant < e.Quadrant)
			{
				return -1;
			}
			return CGAlgorithms.ComputeOrientation(e.p0, e.p1, this.p1);
		}

		public void Write(StreamWriter outstream)
		{
			string fullName = base.GetType().FullName;
			int num = fullName.LastIndexOf('.');
			string text = fullName.Substring(num + 1);
			outstream.Write(string.Concat(new object[]
			{
				"  ",
				text,
				": ",
				this.p0,
				" - ",
				this.p1,
				" ",
				this._quadrant,
				":",
				this._angle
			}));
		}

		public override bool IsRemoved
		{
			get
			{
				return this.parentEdge == null;
			}
		}

		internal void Remove()
		{
			this._sym = null;
			this.parentEdge = null;
		}

		public override string ToString()
		{
			return string.Concat(new object[]
			{
				"DirectedEdge: ",
				this.p0,
				" - ",
				this.p1,
				" ",
				this._quadrant,
				":",
				this._angle
			});
		}

		protected Edge parentEdge;

		protected Node from;

		protected Node to;

		protected Coordinate p0;

		protected Coordinate p1;

		private DirectedEdge _sym;

		private readonly int _quadrant;

		private readonly double _angle;
	}
}
